﻿Partial Public Class frmMaster
    Inherits System.Web.UI.Page

    Public Enum cmbTipoBusqueda
        Texto = 1
        Valor = 2
    End Enum

#Region " Propiedades "
    Public Property RUT() As String
        Get
            Return IIf(Session("RUT") Is Nothing, String.Empty, Session("RUT"))
        End Get
        Set(ByVal value As String)
            Session("RUT") = value.Trim
        End Set
    End Property

    Public Property RUTCliente() As String
        Get
            Return IIf(Session("RUTCliente") Is Nothing, String.Empty, Session("RUTCliente"))
        End Get
        Set(ByVal value As String)
            Session("RUTCliente") = value.Trim
        End Set
    End Property

    Public Property NombreCliente() As String
        Get
            Return IIf(Session("NombreCliente") Is Nothing, String.Empty, Session("NombreCliente"))
        End Get
        Set(ByVal value As String)
            Session("NombreCliente") = value.Trim
        End Set
    End Property

    Public Property NroSeguro() As String
        Get
            Return IIf(Session("NroSeguro") Is Nothing, String.Empty, Session("NroSeguro"))
        End Get
        Set(ByVal value As String)
            Session("NroSeguro") = value.Trim
        End Set
    End Property

    Public Property Patente() As String
        Get
            Return IIf(Session("Patente") Is Nothing, String.Empty, Session("Patente"))
        End Get
        Set(ByVal value As String)
            Session("Patente") = value.Trim
        End Set
    End Property

    Public Property TipoVehiculo() As String
        Get
            Return IIf(Session("TipoVehiculo") Is Nothing, String.Empty, Session("TipoVehiculo"))
        End Get
        Set(ByVal value As String)
            Session("TipoVehiculo") = value.Trim
        End Set
    End Property

    Public Property Marca() As String
        Get
            Return IIf(Session("Marca") Is Nothing, String.Empty, Session("Marca"))
        End Get
        Set(ByVal value As String)
            Session("Marca") = value.Trim
        End Set
    End Property

    Public Property Modelo() As String
        Get
            Return IIf(Session("Modelo") Is Nothing, String.Empty, Session("Modelo"))
        End Get
        Set(ByVal value As String)
            Session("Modelo") = value.Trim
        End Set
    End Property

    Public Property Anio() As String
        Get
            Return IIf(Session("Anio") Is Nothing, String.Empty, Session("Anio"))
        End Get
        Set(ByVal value As String)
            Session("Anio") = value.Trim
        End Set
    End Property

    Public Property Compania() As String
        Get
            Return IIf(Session("Compania") Is Nothing, String.Empty, Session("Compania"))
        End Get
        Set(ByVal value As String)
            Session("Compania") = value.Trim
        End Set
    End Property

    Public Property NroMotor() As String
        Get
            Return IIf(Session("NroMotor") Is Nothing, String.Empty, Session("NroMotor"))
        End Get
        Set(ByVal value As String)
            Session("NroMotor") = value.Trim
        End Set
    End Property

    Public Property RUTPropietario() As String
        Get
            Return IIf(Session("RUTPropietario") Is Nothing, String.Empty, Session("RUTPropietario"))
        End Get
        Set(ByVal value As String)
            Session("RUTPropietario") = value.Trim
        End Set
    End Property

    Public Property NombrePropietario() As String
        Get
            Return IIf(Session("NombrePropietario") Is Nothing, String.Empty, Session("NombrePropietario"))
        End Get
        Set(ByVal value As String)
            Session("NombrePropietario") = value.Trim
        End Set
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Email() As String
        Get
            Return IIf(Session("Email") Is Nothing, String.Empty, Session("Email"))
        End Get
        Set(ByVal value As String)
            Session("Email") = value.Trim
        End Set
    End Property

    Public Property ColorCliente() As String
        Get
            Return IIf(Session("ColorCliente") Is Nothing, String.Empty, Session("ColorCliente"))
        End Get
        Set(ByVal value As String)
            Session("ColorCliente") = value.Trim
        End Set
    End Property
#End Region

#Region " Eventos "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Anthem.Manager.Register(Me)
    End Sub
#End Region

#Region " Funciones "

    Protected Function ValidarEmail(ByVal Email As String) As Boolean
        Dim pattern As String
        pattern = "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

        Return Regex.IsMatch(Email, pattern)
    End Function

    Protected Function ValidarPatente(ByVal Patente As String) As Boolean
        Dim MiRespuesta As Boolean = False

        If IsNumeric(Right(Patente, 4)) And Char.IsLetter(Left(Patente, 2)) Then
            MiRespuesta = True
        ElseIf Char.IsLetter(Right(Patente, 2)) And IsNumeric(Left(Patente, 4)) Then
            MiRespuesta = True
        End If

        Return MiRespuesta
    End Function

    Protected Function ValidaRut(ByRef RutVerificar As Object) As Boolean
        Return ValidaRut(CType(RutVerificar, Anthem.TextBox).Text)
    End Function

    Protected Function ValidaRut(ByVal RutVerificar As String) As Boolean
        Dim rutLimpio As String
        Dim digitoVerificador As String
        Dim suma As Integer
        Dim contador As Integer = 2
        Dim valida As Boolean = True
        Dim retorno As Boolean = False

        rutLimpio = RutVerificar.Replace(".", "")
        rutLimpio = rutLimpio.Replace("-", "")
        rutLimpio = rutLimpio.Replace(" ", "")
        rutLimpio = rutLimpio.Substring(0, rutLimpio.Length - 1)
        digitoVerificador = RutVerificar.Substring(RutVerificar.Length - 1, 1)

        Dim i As Integer

        For i = rutLimpio.Length - 1 To 0 Step -1
            If contador > 7 Then
                contador = 2
            End If

            Try
                suma += Integer.Parse(rutLimpio(i).ToString()) * contador
                contador += 1
            Catch ex As Exception
                valida = False
            End Try
        Next

        If valida Then
            Dim dv As Integer = 11 - (suma Mod 11)
            Dim DigVer As String = dv.ToString()

            If DigVer = "10" Then
                DigVer = "K"
            End If

            If DigVer = "11" Then
                DigVer = "0"
            End If

            If DigVer = digitoVerificador.ToUpper Then
                retorno = True
            Else
                retorno = False
            End If
        End If

        Return retorno
    End Function
#End Region

#Region " Métodos "
    Protected Sub MsgError(ByVal Mensaje As String)
        Mensaje = Mensaje.Replace("'", "").Replace("<", "").Replace(">", "")

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & Mensaje & "');")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alert_call", "<script>alert('" & Mensaje & "');</script>")
        End If
    End Sub

    'Protected Sub MsgError(ByVal Mensaje As String)
    '    Mensaje = Mensaje.Replace("'", "").Replace("<", "").Replace(">", "")

    '    If Anthem.Manager.IsCallBack Then
    '        Anthem.Manager.AddScriptForClientSideEval("jError('" & Mensaje & "', 'Error');")
    '    Else
    '        Page.ClientScript.RegisterStartupScript(Me.GetType, "jError_call", "<script>jError('" & Mensaje & "', 'Error');</script>")
    '    End If
    'End Sub

    Protected Sub MsgInformativo(ByVal Mensaje As String)
        Mensaje = Mensaje.Replace("'", "").Replace("<", "").Replace(">", "")

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & Mensaje & "');")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alert_call", "<script>alert('" & Mensaje & "');</script>")
        End If
    End Sub

    'Protected Sub MsgInformativo(ByVal Mensaje As String)
    '    Mensaje = Mensaje.Replace("'", "").Replace("<", "").Replace(">", "")

    '    If Anthem.Manager.IsCallBack Then
    '        Anthem.Manager.AddScriptForClientSideEval("jAlert('" & Mensaje & "', 'Información');")
    '    Else
    '        Page.ClientScript.RegisterStartupScript(Me.GetType, "jAlert_call", "<script>jAlert('" & Mensaje & "', 'Información');</script>")
    '    End If
    'End Sub

    Protected Sub MsgConfirm(ByVal Mensaje As String, ByVal funcionJ As String)
        Mensaje = Mensaje.Replace("'", "").Replace("<", "").Replace(">", "")

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("confirm('" & Mensaje & "');")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "confirm_call", "<script>confirm('" & Mensaje & "');</script>")
        End If
    End Sub

    'Protected Sub MsgConfirm(ByVal Mensaje As String, ByVal funcionJ As String)
    '    Mensaje = Mensaje.Replace("'", "").Replace("<", "").Replace(">", "")

    '    If Anthem.Manager.IsCallBack Then
    '        Anthem.Manager.AddScriptForClientSideEval("jConfirm('" & Mensaje & "', 'Confirmación');")
    '    Else
    '        Page.ClientScript.RegisterStartupScript(Me.GetType, "jConfirm_call", "<script>jConfirm('" & Mensaje & "', 'Confirmación');</script>")
    '    End If
    'End Sub

    Protected Sub ExecuteSP(ByRef rp As DotNetResponse.SQLPersistence, ByVal commit As Boolean, ByVal returnValue As Boolean)
        Dim exec As New DotNetDal.SQLPersistence(commit, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        If returnValue Then
            exec.ExecuteDataTable(rp)
            If commit Then
                exec.Commit()
            End If
        Else
            exec.ExecuteNonQuery(rp)
            exec.Commit()
        End If
    End Sub

    Public Sub SendMail(ByVal MailMessage As Net.Mail.MailMessage)
        SendMail(MailMessage, ConfigurationManager.AppSettings("Correo.Servidor"), ConfigurationManager.AppSettings("Correo.Credencial").Split("|")(0), ConfigurationManager.AppSettings("Correo.Credencial").Split("|")(1))
    End Sub

    ''' <summary>
    ''' Sends mail using SMTP client
    ''' </summary>
    ''' <param name="MailMesagge">Mensaje de correo</param>
    ''' <param name="MailServer">The SMTP server (MailServer) as String</param>
    ''' <remarks>It can use the IP of Server also</remarks>
    Protected Sub SendMail(ByVal MailMesagge As Net.Mail.MailMessage, ByVal MailServer As String, ByVal CorreoEnvia As String, ByVal ClaveEnvia As String)
        'send the message using SMTP client
        Dim smtp As New Net.Mail.SmtpClient(MailServer) 'mail Server IP or NAME 
        smtp.Credentials = New System.Net.NetworkCredential(CorreoEnvia, ClaveEnvia)
        Try
            smtp.Send(MailMesagge)
        Catch ex As Exception
            MsgError("No fué posible enviar el correo.-")
        End Try
    End Sub ' End SendMail

    Protected Sub CargaDDL(ByRef ddl As Anthem.DropDownList, ByVal SP As String, ByVal CampoDato As String, ByVal CampoValor As String, Optional ByVal parametros() As String = Nothing)
        Dim rp As New DotNetResponse.SQLPersistence

        If Not parametros Is Nothing Then
            rp.Parametro = parametros
        End If

        rp.StoredProcedure = SP

        ExecuteSP(rp, False, True)

        ddl.Items.Clear()
        ddl.DataSource = Nothing
        If Not rp.Errores And Not rp.DtTable Is Nothing Then
            ddl.DataTextField = CampoValor
            ddl.DataValueField = CampoDato
            ddl.DataSource = rp.DtTable
            ddl.DataBind()
        ElseIf rp.Errores Then
            MsgError(rp.MensajeError)
        End If

        ddl.Items.Insert(0, New ListItem("Seleccione", "-999"))

        ddl.UpdateAfterCallBack = True

    End Sub

    Protected Sub cmbBuscaValorSelecciona(ByRef ddl As Anthem.DropDownList, ByVal ValorBuscado As String)
        cmbBuscaTextoSelecciona(ddl, cmbTipoBusqueda.Valor, ValorBuscado)
    End Sub

    Protected Sub cmbBuscaTextoSelecciona(ByRef ddl As Anthem.DropDownList, ByVal TextoBuscado As String)
        cmbBuscaTextoSelecciona(ddl, cmbTipoBusqueda.Texto, TextoBuscado)
    End Sub

    Private Sub cmbBuscaTextoSelecciona(ByRef ddl As Anthem.DropDownList, ByVal TipoBusqueda As cmbTipoBusqueda, ByVal Busqueda As String)
        Select Case TipoBusqueda
            Case 1
                If Not ddl.Items.FindByText(Busqueda) Is Nothing Then
                    ddl.SelectedValue = ddl.Items.FindByText(Busqueda).Value
                    ddl.UpdateAfterCallBack = True
                End If
            Case 2
                If Not ddl.Items.FindByValue(Busqueda) Is Nothing Then
                    ddl.SelectedValue = ddl.Items.FindByValue(Busqueda).Value
                    ddl.UpdateAfterCallBack = True
                End If
        End Select
    End Sub

    Public Sub IrA(ByVal Pagina As String)
        IrA(Pagina, True)
    End Sub

    Private Sub IrA(ByVal Pagina As String, ByVal TerminaEjecucion As Boolean)
        Response.Redirect(Pagina, TerminaEjecucion)
    End Sub
#End Region
End Class